package com.util
{
	
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;


public class TestButton extends Sprite
{

	private var _label:TextField;
	private var _bg:Shape;
	
	public var onClick:Function;

	public function TestButton( label:String=null )
	{
		this.mouseChildren = false;
		this.buttonMode = true;
		
		_bg = new Shape();
		_bg.graphics.beginFill( 0x0000ff );
		_bg.graphics.drawRect( 0, 0, 1, 1);
		_bg.name = "bg";
		addChild( _bg );
		
		_label = new TextField();
		_label.x = 10;
		_label.autoSize = TextFieldAutoSize.LEFT;
		_label.selectable = false;
		_label.textColor = 0xffffff;
		addChild( _label );
		
		this.label = label;
		this.addEventListener( MouseEvent.CLICK, delegateClick );
		this.addEventListener( MouseEvent.MOUSE_DOWN, onMouseDown );
	}
	
	private function onMouseDown( evt:MouseEvent ):void
	{
		evt.stopPropagation();
	}
	
	dynamic public function delegateClick( evt:MouseEvent ):void
	{
		if( onClick != null ) onClick.call();
	}
	
	public function set label( value:String ):void
	{
		_label.text = value;
		
		//_bg.width = _label.textWidth + 20;
		_bg.height = 20;
	}
	
	override public function set width( value:Number ):void
	{
		_bg.width = value;
		_label.x = Math.round( value/2 - _label.textWidth/2 );
	}
	
}
}